package com.yixin.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.DrugFactory;
import com.yixin.hospital.entity.dto.req.drugfactory.DrugFactoryAddReqDTO;
import com.yixin.hospital.entity.dto.req.drugfactory.DrugFactoryEditReqDTO;
import com.yixin.hospital.service.DrugFactoryService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/drug-factory")
@Api(value = "药品生产厂家管理", tags = "药品生产厂家管理")
@Slf4j
public class DrugFactoryController {

    @Resource
    DrugFactoryService drugFactoryService;

    @GetMapping("/list")
    @ApiOperation(value = "生产厂家列表")
    public ApiResult<?> list() {
        return ApiResult.success(drugFactoryService.list());
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加生产厂家")
    public ApiResult<?> add(@Valid @RequestBody DrugFactoryAddReqDTO drugFactoryAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        DrugFactory drugFactory = new DrugFactory();
        drugFactory.setFactory(drugFactoryAddReqDTO.getFactory());
        drugFactory.setCreatedBy(userName);
        drugFactory.setUpdatedBy(userName);
        drugFactoryService.save(drugFactory);
        return ApiResult.success();
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑生产厂家")
    public ApiResult<?> edit(@Valid @RequestBody DrugFactoryEditReqDTO drugFactoryEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        DrugFactory drugFactory = new DrugFactory();
        drugFactory.setId(drugFactoryEditReqDTO.getId());
        drugFactory.setFactory(drugFactoryEditReqDTO.getFactory());
        drugFactory.setUpdatedBy(userName);
        drugFactoryService.updateById(drugFactory);
        return ApiResult.success();
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除生产厂家")
    public ApiResult<?> remove(@PathVariable int id) {
        drugFactoryService.removeById(id);
        return ApiResult.success();
    }
}
